// 引入定义仓库的方法
import { reqHospitalDepartment, reqHospitalDetail } from '@/api/hospital'
import type { Department, Hospital } from '@/api/hospital/type'
import type { BookingRule } from '@/api/hospital/type'
import { ElMessage } from 'element-plus'
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 创建存储医院详情的仓库并暴露
// defineStore("仓库唯一标识",set函数/配置对象) 可以采用组合式API也可采用选项式API写法
const useHospitalDetailsStore = defineStore('hospitalDetails', () => {
  // 存储医院详情的数据
  let hospitalDetail = ref<Hospital>()
  // 存储医院的预约规则
  let bookingRule = ref<BookingRule>()
  // 存储当前医院科室的信息
  let hospitalDepartment = ref<Department[]>([])

  // 获取医院详情的方法,需要接收医院code
  const getHospitalDetails = async (hosCode: string) => {
    // 使用前路由query属性中的医院code获取当前医院详情数据
    const result = await reqHospitalDetail(hosCode)
    if (result.code === 200) {
      // 将医院详情数据存储到仓库中,便于子路由组件的使用
      hospitalDetail.value = result.data.hospital
      bookingRule.value = result.data.bookingRule
    } else {
      ElMessage.error('获取医院详情失败')
    }
  }

  // 获取当前医院科室的方法
  const getHospitalDepartment = async (hosCode: string) => {
    let result = await reqHospitalDepartment(hosCode)
    if (result.code == 200) {
      // 将医院科室的数据存储到仓库中
      hospitalDepartment.value = result.data
    } else {
      ElMessage.error('获取医院科室失败')
    }
  }

  // 将仓库中的数据和方法进行暴露
  return {
    hospitalDetail,
    bookingRule,
    hospitalDepartment,
    getHospitalDetails,
    getHospitalDepartment,
  }
})
// 暴露获取仓库到的方法
export default useHospitalDetailsStore
